<template>
    <customNavigationBar title="支付方式" />

    <view class="money">￥{{ price }}</view>

    <view class="methods">
        <view class="item" v-for="(item, index) in methods" :key="index" @click="checkedMethod = item.value">
            <view class="selectBox">
                <view class="option">
                    <image :src="item.description"></image>
                    <span>{{ item.label }}</span>
                </view>

                <view class="checkBox" :class="item.value == checkedMethod && 'checked'">
                    <image src="/static/common/icon_checked_white.png"></image>
                </view>
            </view>

            <view class="selectBankBox" v-if="item.value > 4 && checkedMethod == item.value && bankNameShow">
                <view class="bank" v-for="(items, indexs) in bankName" :key="items" @click="selectBankName = items">
                    <text>{{ items }}</text>

                    <view class="checkBox2" :class="selectBankName === items && 'checked'">
                        <image src="/static/common/icon_checked_white.png"></image>
                    </view>
                </view>
            </view>
        </view>
    </view>

    <view class="bottom">
        <view class="submit" @click="payConfirm">支付</view>
    </view>
</template>

<script setup lang="ts">
import { ref, watch, watchEffect } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import { commonApis, financeApis, homeApis } from '../../utils/apis';
import _config from '@/config/env.js';
const price = ref(0);
onLoad((e) => {
    price.value = e.price;
});

onShow(() => {
    getPayMethods();
    if (flag.value) {
        getAgencyPayment();
    }
});
const flag = ref(false);
const getAgencyPayment = async () => {
    uni.showLoading();

    let res = await homeApis.getAgencyPayment();

    uni.hideLoading();

    if (res.data.agentAmount === res.data.payAmount) {
        uni.showToast({
            icon: 'none',
            title: '代理费支付成功'
        });
        setTimeout(() => {
            uni.switchTab({
                url: '/pages/home/home'
            });
        }, 1500);
    } else {
        if (res.data.payAmount === price.value) {
            uni.showToast({
                icon: 'none',
                title: '代理费支付成功'
            });
            setTimeout(() => {
                uni.switchTab({
                    url: '/pages/home/home'
                });
            }, 1500);
        } else {
            // uni.showToast({
            //     icon: 'none',
            //     title: '支付失败'
            // });
        }
    }
};

const checkedMethod = ref(null);

watchEffect(() => {
    if (checkedMethod.value < 5) {
        bankNameShow.value = false;
    } else {
        bankNameShow.value = true;
    }
});
const bankNameShow = ref(false);
const selectBankName = ref('');
const bankName = ref(['中国工商银行储蓄卡', '中国农业银行储蓄卡']);

const getPayMethods = () => {
    commonApis.getPayMethods().then((res) => {
        methods.value = res.data;
    });
};

const methods = ref([
    // {
    //     icon: '/static/common/icon_pay_vx.png',
    //     title: '微信支付',
    //     type: 3
    // },
    // {
    //     icon: '/static/common/icon_pay_zfb.png',
    //     title: '支付宝支付',
    //     type: 4
    // },
    // {
    //     icon: '/static/common/icon_payMethod_bank.jpg',
    //     title: '企业网银支付',
    //     type: 6
    // },
    // {
    //     icon: '/static/common/icon_payMethod_bank.jpg',
    //     title: '个人网银支付',
    //     type: 5
    // }
]);

const payConfirm = async () => {
    if (!checkedMethod.value) {
        uni.showToast({
            icon: 'none',
            title: '请选择支付方式后重试'
        });
        return;
    }

    await financeApis
        .zfbPay({
            appSchema: 'yjbsagent://',
            payAmount: price.value,
            type: checkedMethod.value
        })
        .then((res) => {
            flag.value = true;

            if (checkedMethod.value == 4) {
                plus.runtime.openURL(res.data.payCode);
            }

            if (checkedMethod.value == 3) {
                plus.share.getServices((result) => {
                    let sweixin = null;
                    for (let i in result) {
                        sweixin = result[i];
                    }
                    //唤醒微信小程序
                    if (sweixin) {
                        sweixin.launchMiniProgram({
                            id: 'gh_7a5269bbce93', //微信公众平台-设置-最下面的原始id
                            type: _config.basePath.includes('gglucky') ? 1 : 0, //小程序版本  0-正式版； 1-测试版； 2-体验版。
                            path: `/package_user/rechargeWithdrawal/rechargeWithdrawal?orderType=1&outTradeNo=${res.data.orderNo}&amount=${res.data.amount}` //小程序的页面,用传的参数在小程序接值判断跳转指定页面
                        });
                    } else {
                        uni.showModal({
                            title: '检测到您未安装微信，请选择其他方式进行支付'
                        });
                    }
                });
            }
        });
};
</script>

<style lang="scss" scoped>
.money {
    padding: 176rpx 0 160rpx;
    text-align: center;
    font-size: 70rpx;
    line-height: 70rpx;
    color: #2a2a2a;
}

.methods {
    padding: 0 48rpx;

    .item {
        padding: 40rpx 0;
        border-bottom: 2rpx solid #e4e4e6;
        .selectBox {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .selectBankBox {
            padding-left: 40rpx;
            .bank {
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-top: 20rpx;
            }

            text {
                margin-left: 20rpx;
                font-size: 28rpx;
                font-weight: normal;
                line-height: 28rpx;
                letter-spacing: normal;
                color: #333333;
            }

            .checkBox2 {
                width: 30rpx;
                height: 30rpx;
                border: 2rpx solid #707070;
                display: flex;
                align-items: center;
                justify-content: center;
                border-radius: 50%;
                transition: 0.3s;
                image {
                    width: 21rpx;
                    height: 15rpx;
                    opacity: 0;
                }
                &.checked {
                    width: 30rpx;
                    height: 30rpx;
                    background: #2c72ff;
                    border: 2rpx solid #2c72ff;
                    image {
                        opacity: 1;
                    }
                }
            }
        }

        .option {
            display: flex;
            align-items: center;
            image {
                width: 48rpx;
                height: 48rpx;
            }
            span {
                margin-left: 20rpx;
                font-size: 28rpx;
                font-weight: normal;
                line-height: 28rpx;
                letter-spacing: normal;
                color: #333333;
            }
        }

        .checkBox {
            width: 40rpx;
            height: 40rpx;
            border: 2rpx solid #707070;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            transition: 0.3s;
            image {
                width: 21rpx;
                height: 15rpx;
                opacity: 0;
            }
            &.checked {
                width: 40rpx;
                height: 40rpx;
                background: #2c72ff;
                border: 2rpx solid #2c72ff;
                image {
                    opacity: 1;
                }
            }
        }
        &:last-child {
            border: none;
        }
    }
}

.bottom {
    position: fixed;
    bottom: env(safe-area-inset-bottom);
    left: 0;
    width: 100%;
    padding: 30rpx 24rpx;

    .submit {
        width: 100%;
        border-radius: 40rpx;
        background: #2c72ff;
        height: 80rpx;
        font-size: 28rpx;
        color: #ffffff;
        line-height: 80rpx;
        text-align: center;
    }
}
</style>
